#include "RigidCircle.h"
#include "RigidPoly.h"

namespace poly
{
	RigidCircle::RigidCircle() : radius( 0 )
	{
	}

	RigidCircle::RigidCircle( D3DDevice d3ddev,
		RigidBody const & rb, uint32 segments, float r, float4 const & color )
	{
		setup( d3ddev, rb, segments, r, color );
	}

	void RigidCircle::step(float dt)
	{
		rigidBody.step(dt);
	}

	void RigidCircle::render(float dt)
	{
		m_view.render( rigidBody.world() );
	}

	void RigidCircle::setup( D3DDevice device, RigidBody const & rb,
			uint32 segments, float r, float4 const & color )
	{
		rigidBody = rb;
		radius = r;

		m_view.initialize( device );
		m_view.setup( radius, segments, color );
	}

	void RigidCircle::collide( IRigidCollider * collider )
	{
		collider->collide( this );
	}

	void RigidCircle::collide( RigidPoly * rp )
	{
		rp->collide( this );
	}

	void RigidCircle::collide( RigidCircle * rc )
	{
		float circle1Radius = radius;
		float3 circle1Center = rigidBody.pos;

		float circle2Radius = rc->radius;
		float3 circle2Center = rc->rigidBody.pos;

		Contact contact;
		if( intersect( contact,
			circle1Center, circle1Radius,
			circle2Center, circle2Radius ) )
		{
			rigidBody.displace( -contact.seperation / 2 );
			rc->rigidBody.displace( +contact.seperation / 2 );

			rigidBody.resolveCollision( rc->rigidBody,
				contact );
		}
	}

}
